package com.zwb.boot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwb.boot.common.exception.CustomException;
import com.zwb.boot.entity.Category;
import com.zwb.boot.mapper.CategoryMapper;
import com.zwb.boot.service.CategoryService;
import com.zwb.boot.service.DishService;
import com.zwb.boot.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 头发又黑又长
 * @Date 2022/7/22 10:44
 * @email zwb15083976291@163.com
 */

@Slf4j
@Service
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    public void remove(Long id) {
        if (dishService.countByCategoryId(id) != 0) {
            throw new CustomException("当前分类下关联了菜品，不能删除该分类");
        }
        if (setmealService.countByCategoryId(id) > 0) {
            throw new CustomException("当前套餐下关联了菜品，不能删除该分类");
        }
        // 删除分类
        this.removeById(id);
    }

}
